function h=def_draw_game_trace(G)
% def_draw_game_trace: Default subroutine to draw the game trace
%
% SYNTAX:
%   h = def_draw_game_trace(G)
%   Return handles of graphics objects to represent the game trace in each game step.
%   Such graphics objects will be automatically cleaned in next game step.
%   

%   Author: Hongbin Ma
%   Last updated: 2009-6-1 with MATLAB 6.5
%   Problems or suggestions? Email me: mathmhb[at]163.com

h=[];hh=[];leg={};
if G.draw>1,
    if ~isempty(G.plot_components),
        hh=draw_each_component(G);
    else,
        hh=draw_all_in_one(G);
    end;
    figure(G.figure);
end;

function hh=draw_each_component(G)
hh=[];
if isempty(G.plot_components),
    return;
end;
m=size(G.plot_components,1);
n=size(G.plot_components,2);
fig=G.figure2;
for i1=1:m,
for i2=1:n,
    j=G.plot_components(i1,i2);
    if j<1,
        continue;
    end;
    if m>1,
        figure(G.figure2);
        subplot(m,n,(i1-1)*n+i2);
    else,
        figure(fig);fig=fig+1;
    end;
    hold on;
    title(G.plot_titles{j});
    xlabel(G.plot_xlabel);
    ylabel(G.plot_labels{j});
    for k=1:G.N,
        vv=plot(G.TData,G.P{k}.VarsData(:,j)');
        hh=[hh;vv];
    end;
end;
end;
figure(G.figure);

function hh=draw_all_in_one(G)
    hh=[];leg={};
    figure(G.figure2);
    hold on;
    for k=1:G.N,
        jj=G.P{k}.plot_components;
        ll=G.P{k}.plot_labels;
        if isempty(jj),
            jj=1:size(G.P{k}.VarsData,2);
        end;
        for j=length(ll)+1:size(G.P{k}.VarsData,2);
            ll{j}=['P',int2str(k),'[',int2str(j),']'];
        end;
        %~ disp(G.P{k}.VarsData(:,jj));
        vv=plot(G.TData,G.P{k}.VarsData(:,jj)',G.P{k}.legend);
        hh=[hh;vv];
        leg={leg{:},ll{jj}};
    end;
    legend(hh,leg{:});
    title('history of all states vs. time');
    if ~isempty(G.axis2),
        axis(G.axis2);
    end;
